//Handling Keyboard Events F---
function HandlingKeyboard()
 {

    }

HandlingKeyboard.prototype.Initialize = function()
 {
    if (document.addEventListener)
    {
        try
        {
            document.removeEventListener("onkeydown", this.HandleKeyDown);
            document.removeEventListener("onmouseup", this.HandleClick);
            document.removeEventListener("onmousedown", this.HandleClick);
            document.removeEventListener("ondblclick", this.HandleDoubleClick);
            document.removeEventListener("onclick", this.HandleDoubleClick);
            document.removeEventListener("onmouseover", this.HandleMouseOver);

        } catch(e)
        {
            }

        document.addEventListener("keydown", this.HandleKeyDown, false);
        //document.addEventListener("keypress",this.HandleKeyPress,false);
        //document.addEventListener("keyup"   ,this.HandleKeyUp,false);
        document.addEventListener("mouseup", this.HandleClick, false);
        document.addEventListener("mousedown", this.HandleClick, false);
        document.addEventListener("dblclick", this.HandleDoubleClick, false);
        document.addEventListener("click", this.HandleDoubleClick, false);
        document.addEventListener("mouseover", this.HandleMouseOver, false);
    }
    else if (document.attachEvent)
    {
        try
        {
            document.detachEvent("onkeydown", this.HandleKeyDown);
            //document.detachEvent("onkeypress",this.HandleKeyPress);
            //document.detachEvent("onkeyup"   ,this.HandleKeyUp);
            document.detachEvent("onmouseup", this.HandleClick);
            document.detachEvent("onmousedown", this.HandleClick);
            document.detachEvent("ondblclick", this.HandleDoubleClick);
            document.detachEvent("onclick", this.HandleDoubleClick);
            document.detachEvent("onmouseover", this.HandleMouseOver);
            window.status = "deattachEvent";
        } catch(e)
        {
            }

        document.attachEvent("onkeydown", this.HandleKeyDown);
        //document.attachEvent("onkeypress",this.HandleKeyPress);
        //document.attachEvent("onkeyup"   ,this.HandleKeyUp);
        document.attachEvent("onmouseup", this.HandleClick);
        document.attachEvent("onmousedown", this.HandleClick);
        document.attachEvent("ondblclick", this.HandleDoubleClick);
        document.attachEvent("onclick", this.HandleDoubleClick);
        document.attachEvent("onmouseover", this.HandleMouseOver);
    }
    else
    {
        document.onkeydown = this.HandleKeydown;
        //document.onkeypress=this.HandleKeypress;
        //document.onkeyup   =this.HandleKeyup;
    }
}

HandlingKeyboard.prototype.AddKBSetting = function(oKeyPress, XElement)
 {
    ZZUKB_SETTING[oKeyPress] = XElement;
};

HandlingKeyboard.prototype.AddKBObject = function(oKeyPress, XElement)
 {
    ZZUKB_OBJECT[oKeyPress] = XElement;
};

HandlingKeyboard.prototype.HandleKeyUp = function(event)
 {
    var oXEvent = new XEvent(event);
    var eL = oXEvent.XElement;
    var _Name = eL.getAttribute("wxf");
    var kc = oXEvent.keyCode;

    //alert("HandleKeyUp");
    //ZZTrace('KeyUp  keyCode=' + event.keyCode + ' which=' + event.which + ' charCode=' + event.charCode+' keyIdentifier='+ event.keyIdentifier + ' keyLocation='+event.keyLocation+' shiftKey='+event.shiftKey + ' ctrlKey='+event.ctrlKey + ' altKey='+event.altKey + ' metaKey='+event.metaKey);
    if (_Name)
    {
        var eL2 = document.getElementById(_Name + "_viewport");
        if (eL2)
        {
            if (eL2.Wxf)
            {
                var oWxfJs = eL2.Wxf;
                if (oWxfJs)
                {
                    return oWxfJs.UpdateNode();
                }
            }
        }
    }
};

HandlingKeyboard.prototype.HandleKeyPress = function(event)
 {
    var oXEvent = new XEvent(event);
    var eL = oXEvent.XElement;
    var kc = oXEvent.keyCode;

    //alert("HandleKeyPress");
    //window.status=oXEvent.Type+" "+kc;
    //ZZTrace('KeyPress  keyCode=' + event.keyCode + ' which=' + event.which + ' charCode=' + event.charCode+' keyIdentifier='+ event.keyIdentifier + ' keyLocation='+event.keyLocation+' shiftKey='+event.shiftKey + ' ctrlKey='+event.ctrlKey + ' altKey='+event.altKey + ' metaKey='+event.metaKey);
    window.status = "HandleKeyPress " + kc;

    if (kc == 17)
    {
        ZZCTRL_KEY_PRESS = true;
        window.clearTimeout(oCTRL_KEY_PRESS);
        oCTRL_KEY_PRESS = setTimeout("ZZCTRL_KEY_PRESS=false;window.clearTimeout(oCTRL_KEY_PRESS);", DELAY_CTRL_KEY);
        oXEvent.FreezeEvent();
        return false;
    }

    if (ZZCTRL_KEY_PRESS && kc == 74)
    {
        var wxfsuggest = eL.getAttribute("wxfsuggest");
        if ((eL.getAttribute("uget_code")) && (eL.readOnly == false) && (!wxfsuggest))
        {
            oXEvent.FreezeEvent();
            XAutoSuggest(eL.id);
        }
        return false;
    }

    if (eL)
    {
        var datatype = eL.getAttribute("datatype");
        if (datatype == "c")
        {
            var ucase = eL.getAttribute("ucase");
            if (ucase)
            {
                switch (ucase.toUpperCase())
                {
                case "UCASE":
                    if (kc >= 97 && kc <= 122)
                    {
                        oXEvent.FireEvent(kc - 32);
                    }
                    break;
                case "LCASE":
                    if (iKey >= 65 && iKey <= 90)
                    {
                        oXEvent.FireEvent(kc + 32);
                    }
                    break;
                }
            }
        }
        else if (datatype == "f")
        {
            if (kc == 61)
            {
                var point = fGetXY(eL);

                NPressLayer.style.top = point.y - 1;
                NPressLayer.style.left = point.x + 1;
                if (oPreElement != eL.id)
                {
                    document.getElementById('Txt_NExpress').value = "";
                }
                document.getElementById('NPressLayer').style.display = "block";

                oPreElement = eL.id;

                document.getElementById('Txt_NExpress').focus();
                document.getElementById('Txt_NExpress').select();
                oXEvent.FreezeEvent();

            }
            else if (kc == 75 || kc == 107)
            {
                if (eL.value.indexOf('.') == -1)
                {
                    eL.value = eL.value + "000";
                }
                oXEvent.FreezeEvent();
                return false;
            }
            else if ((kc < 48 || kc > 57) && kc != 46 && kc != 45 && kc != 43)
            {
                oXEvent.FreezeEvent();
            }
        }
        else if (datatype == "n")
        {
            if (kc == 61)
            {
                var point = fGetXY(eL);

                NPressLayer.style.top = point.y - 1;
                NPressLayer.style.left = point.x + 1;
                if (oPreElement != eL.id)
                {
                    document.getElementById('Txt_NExpress').value = "";
                }
                document.getElementById('NPressLayer').style.display = "block";

                oPreElement = eL.id;

                document.getElementById('Txt_NExpress').focus();
                document.getElementById('Txt_NExpress').select();
                oXEvent.FreezeEvent();

            }
            else if (kc == 75 || kc == 107)
            {
                eL.value = eL.value + "000";
                oXEvent.FreezeEvent();
                return false;
            }
            else if (kc < 48 || kc > 57)
            {
                oXEvent.FreezeEvent();
                return false;
            }
        }
        else if (datatype == "d")
        {
            if (kc == 84 || kc == 116)
            {
                if (eL.value == "")
                {
                    var today = new Date();
                    eL.value = FormatDate(today, ZZLOGON_DATE_FMT);
                }
                oXEvent.FreezeEvent();
                return false;
            }else if (kc == 77 || kc == 109)
            {
                if (eL.value == "")
                {
                    var today = new Date();
                    var d3 = new Date(today.getFullYear(),today.getMonth(),1,0,0,0)
                    eL.value = FormatDate(d3, ZZLOGON_DATE_FMT);
                }
                oXEvent.FreezeEvent();
                return false;
            }else if (kc == 89 || kc == 121)
            {
                if (eL.value == "")
                {
                    var today = new Date();
                    var d3 = new Date(today.getFullYear(),1,1,0,0,0)
                    eL.value = FormatDate(d3, ZZLOGON_DATE_FMT);
                }
                oXEvent.FreezeEvent();
                return false;
            }
            else if (! (kc == 45 || kc == 47 || (kc >= 48 && kc <= 57)))
            {
                oXEvent.FreezeEvent();
                return false;
            }
        }
        else if (datatype == "m")
        {
            }
        else
        {
            }
    }
};

HandlingKeyboard.prototype.HandleKeyDown = function(event)
 {
    var oXEvent = new XEvent(event);
    var eL = oXEvent.XElement;
    var kc = oXEvent.keyCode;
    var _Name = "";

    window.status = oXEvent.Type + " " + kc + " " + eL.id;

    //alert("HandleKeyDown");
    if (kc == 17)
    {
        ZZCTRL_KEY_PRESS = true;
        window.clearTimeout(oCTRL_KEY_PRESS);
        oCTRL_KEY_PRESS = setTimeout("ZZCTRL_KEY_PRESS=false;window.clearTimeout(oCTRL_KEY_PRESS);", DELAY_CTRL_KEY);
        oXEvent.FreezeEvent();
        return false;
    }

    if (ZZCTRL_KEY_PRESS && kc == 191)
    {
        var oLookUp = document.getElementById("Txt_Quick_Lookup");
        if (oLookUp)
        {
            oLookUp.focus();
            oLookUp.select();
            return false;
        }
    }

    if (ZZCTRL_KEY_PRESS && kc == 220)
    {

        //alert(_ZZDebugs);
        try
        {
            XWikiMessage("TraceBlock", "", "Debug Log", _ZZDebugs + '<br>' + ZRuntime.join('<br>'));
        }
        catch(e)
        {
            alert(_ZZDebugs);
        }
        return false;
    }

    if (ZZCTRL_KEY_PRESS && kc == 74)
    {
        var wxfsuggest = eL.getAttribute("wxfsuggest");
        if ((eL.getAttribute("uget_code")) && (eL.readOnly == false) && (!wxfsuggest))
        {
            oXEvent.FreezeEvent();
            XAutoSuggest(eL.id);
        }
        return false;
    }
    if (ZZAutocomplete == true)
    {
        var wxfsuggest = eL.getAttribute("wxfsuggest");
        if ((eL.getAttribute("uget_code")) && (eL.readOnly == false) && (!wxfsuggest))
        {
            window.clearTimeout(SuggestTimer);
            //alert();
            SuggestTimer = window.setTimeout("XAutoSuggest('" + eL.id + "');", ZZAutocomplete_DELAY);
        }
    }
    if (eL.tagName.toUpperCase() == "INPUT")
    {
        if (kc == ZDEF_UP)
        {
            PrevSuggest();
            return oXEvent.FreezeEvent();
        }

        if (kc == ZDEF_DOWN)
        {
            NextSuggest();
            return oXEvent.FreezeEvent();
        }
    }
    if (ZZCTRL_KEY_PRESS)
    {
        if (document.getElementById(ZZUKB_SETTING[oXEvent.iChar]) != null)
        {
            if (document.getElementById(ZZUKB_SETTING[oXEvent.iChar]).disabled == false)
            {
                oXEvent.FreezeEvent();
                eval(document.getElementById(ZZUKB_SETTING[oXEvent.iChar]).onclick());
                return false;
            }
        }
        if (ZZUKB_OBJECT[oXEvent.iChar] != undefined)
        {
            try
            {
                oXEvent.FreezeEvent();
                eval(ZZUKB_OBJECT[oXEvent.iChar]);
                return false;
            }
            catch(e)
            {
                }
        }
    }

    if (kc == ZDEF_ENTER)
    {
        switch (eL.tagName.toUpperCase())
        {
        case "SELECT":
            switch (eL.type.toUpperCase())
            {
            case "SELECT-ONE":
                oXEvent.NFocus();
                break;
            default:
            }
        case "INPUT":
            switch (eL.type.toUpperCase())
            {
            case "BUTTON":
                if (eL.value == "...")
                {
                    oXEvent.FireEvent(9);
                }
                else
                {
                    oXEvent.FireEvent(32);
                }
                break;
            case "TEXT":
                _Name = eL.getAttribute("wxf");

                //ZZProfile(_Name);
                if (_Name)
                {
                    eL2 = document.getElementById(_Name + "_viewport");
                    oWxfJs = eL2.Wxf;
                    if (oWxfJs)
                    {
                        if (oSuggest)
                        {
                            oSuggest.IsActive = true;
                            ActivateCurrentMatch();
                            HideWINSuggest();
                        }
                        ZZProfile('ProcessEvent');
                        return oWxfJs.ProcessEvent(oEvent);
                    }
                }
                else if (eL.id == "Txt_Quick_Lookup")
                {
                    var oLookUpEvent = document.getElementById("btn_Quick_Lookup");
                    if (oLookUpEvent)
                    {
                        oLookUpEvent.click();
                    }
                    return false;
                }
                else
                {
                    if (oSuggest)
                    {
                        oSuggest.ActiveBox = true;
                        if (oSuggest.IsSafari)
                        {
                            oSuggest.TextBox.blur();
                        }
                        oSuggest.TextBox.focus();
                        ActivateCurrentMatch();
                        HideWINSuggest();
                    }
                    oXEvent.NFocus();
                }
                break;
            case "RADIO":
                oXEvent.NFocus();
                break;
            case "CHECKBOX":
                oXEvent.NFocus();
                break;
            default:
            }
            break;
        case "TEXTAREA":
            if (ZZCTRL_KEY_PRESS)
            {
                oXEvent.NFocus();
            }
            break;
        }
    }

};

HandlingKeyboard.prototype.XFocus = function(eL)
 {
    if (eL)
    {
        var datatype = eL.getAttribute("datatype");

        if (datatype == "c")
        {
            var events = eL.getAttribute("events");
            if (events && eL.value != "" && (eL.value != null))
            {
                try
                {
                    var evalTimer = setTimeout(events, 500);

                }
                catch(e)
                {
                    alert(e);
                }
            }
        }
        else if (datatype == "d")
        {
            if (eL.value != "")
            {
                if (!IsDate(eL.value))
                {
                    Message_Box(lbl_invalid_date);
                    eL.value = "";
                }
                else
                {
                    if (eL.value)
                    {
                        eL.value = FormatDate(Parse_Date(eL.value), ZZLOGON_DATE_FMT);
                    }
                }
            }
        }
        else if (datatype == "n")
        {
            }
        else if (datatype == "f")
        {
            scale = eL.getAttribute("scale");
            if (scale == null)
            {
                scale = 0;
            }
            NBLF(scale);
        }
    }
};

HandlingKeyboard.prototype.HandleBlur = function(event)
 {
    var oXEvent = new XEvent(event);
    var oEvent = oXEvent.XEvent;
    var eL = oXEvent.XElement;

    if (SuggestTimer)
    {
        window.clearTimeout(SuggestTimer);
    }

    if (oEvent["type"] == "deactivate")
    {
        var eL2 = IsWithinWxf(eL);
        if (eL2)
        {
            var datatype = eL2.getAttribute("datatype");
            if (datatype == 't')
            {
                oWxfJs = eL2.Wxf;
                if (oWxfJs)
                {
                    oWxfJs.XFocus();
                }
            }
        }
    }
    if (oHandlingKeyboard)
    {
        oHandlingKeyboard.XFocus(eL);
    }
    HideWINSuggest();
    return true;
};

HandlingKeyboard.prototype.HandleDoubleClick = function(event)
 {
    var oXEvent = new XEvent(event);
    var oEvent = oXEvent.XEvent;
    var cType = oEvent["type"];
    var eL = oXEvent.XElement;
    if (eL)
    {
        if (cType == "dblclick")
        {
            var wxfsuggest = eL.getAttribute("wxfsuggest");
            if ((eL.getAttribute("uget_code")) && (eL.readOnly == false) && (!wxfsuggest))
            {
                XAutoSuggest(eL.id);
            }
        }

        eL = IsWithinWxf(eL);
        if (eL)
        {
            var datatype = eL.getAttribute("datatype");
            if (datatype == 't')
            {
                oWxfJs = eL.Wxf;
                if (oWxfJs)
                {
                    oWxfJs.setFocus(oEvent);
                    if (cType == "dblclick")
                    {
                        oWxfJs.Edit_Bef();
                    }
                }
            }
        }
    }
};

HandlingKeyboard.prototype.HandleClick = function(event)
 {
    var oXEvent = new XEvent(event);
    var oEvent2 = oXEvent.XEvent;
    var cType = oEvent2["type"];
    var eL2 = oXEvent.XElement;
    var eL = null;
    if (eL2)
    {
        var datatype = eL2.getAttribute("datatype");
        var eL = IsWithinWxf(eL2);
        if (eL)
        {
            datatype = eL.getAttribute("datatype");

            if (datatype == 't' && cType == 'mousedown')
            {
                oWxfJs = eL.Wxf;
                if (oWxfJs)
                {
                    oWxfJs.setFocus(oEvent2);
                }
            }
        }
    }
    if (oSuggest && oSuggest.IsActive)
    {
        }
    if (cType == "mouseup")
    {
        window.document.body.style.cursor = "default";
    }
};

HandlingKeyboard.prototype.HandleMouseOver = function(event)
 {
    var oXEvent = new XEvent(event);
    var oEvent = oXEvent.XEvent;
    var eL = oXEvent.XElement;

};

HandlingKeyboard.prototype.HandleFocus = function(event)
 {
    var oXEvent = new XEvent(event);
    var oEvent = oXEvent.XEvent;
    var eL = oXEvent.XElement;

    if (eL)
    {
        eL = IsWithinWxf(eL);
        if (eL)
        {
            var datatype = eL.getAttribute("datatype");
            if (datatype == 't')
            {
                oWxfJs = eL.Wxf;
                if (oWxfJs)
                {
                    oWxfJs.setFocus(oEvent);
                }
            }
        }
    }
};

HandlingKeyboard.prototype.RegisterEvents = function(oElement)
 {
    oElement.setAttribute("autocomplete", "OFF");
    oElement.onfocus = this.HandleFocus;
    oElement.onblur = this.HandleBlur;

	//$(document).keypress(function(e)
	//{
	//	this.HandleKeyPress(e);
	//}		 	

    if (oElement.tagName != "INPUT")
    {
        //alert(oElement.tagName);
    }

    if (oElement.addEventListener)
    {
        try
        {
            oElement.removeEventListener("keydown", this.HandleKeyDown, false);
            oElement.removeEventListener("keypress", this.HandleKeyPress, false);
            oElement.removeEventListener("keyup", this.HandleKeyUp, false);
        } catch(e)
        {
            }

        oElement.addEventListener("keydown", this.HandleKeyDown, false);
        oElement.addEventListener("keypress", this.HandleKeyPress, false);
        oElement.addEventListener("keyup", this.HandleKeyUp, false);
    } else if (oElement.attachEvent)
    {
        try
        {
            oElement.detachEvent("onkeydown", this.HandleKeyDown);
            oElement.detachEvent("onkeypress", this.HandleKeyPress);
            oElement.detachEvent("onkeyup", this.HandleKeyUp);

            window.status = "deattachEvent";

        } catch(e)
        {
            }

        oElement.attachEvent("onkeydown", this.HandleKeyDown);
        oElement.attachEvent("onkeypress", this.HandleKeyPress);
        oElement.attachEvent("onkeyup", this.HandleKeyUp);

    } else
    {
        oElement.onkeydown = this.HandleKeydown;
        oElement.onkeypress = this.HandleKeypress;
        oElement.onkeyup = this.HandleKeyup;
    }
};

HandlingKeyboard.prototype.RegisterWxf = function(oElement)
 {
    var oWxfJs = new WxfJs();
    oWxfJs.Initialize(oElement);
    oElement.Wxf = oWxfJs;
    //this.RegisterEvents(oElement);
}

HandlingKeyboard.prototype.RegisterListeners = function()
 {
    var Inputs = document.getElementsByTagName("input");
    var Texts = document.getElementsByTagName("textarea");
    var Tables = document.getElementsByTagName("table");
    z = 0;
    y = 0;
    x = 0;
    $('input').each(function() 
    {
    	if ($(this).attr("datatype"))
    	{
	    	//alert($(this).attr("datatype"));
	    	//$('#foo').bind('click', handler);
			//$(this).bind("keypress",this.HandleKeyPress);
	    	//$(this).keydown(this.HandleKeydown); 
	    	//$(this).keyup(this.HandleKeyup); 
		}
	});
    while (Inputs[z])
    {
        if (Inputs[z].getAttribute("datatype") != null)
        {
            this.RegisterEvents(Inputs[z]);
        }
        z++;
    }
    while (Texts[y])
    {
        if (Texts[y].getAttribute("datatype") != null)
        {
            this.RegisterEvents(Texts[y]);
        }
        y++;
    }
    //alert("RegisterListeners");
    while (Tables[x])
    {
        if (Tables[x].getAttribute("datatype") != null)
        {
            this.RegisterWxf(Tables[x]);
        }
        x++;
    }
};
//Handling Keyboard Events E---
